package com.yyq.eduservice.controller.frontIndex;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yyq.eduservice.client.orderClient;
import com.yyq.eduservice.entity.EduCourse;
import com.yyq.eduservice.entity.chapter.chapter_1;
import com.yyq.eduservice.entity.frontVo.courseFrontVo;
import com.yyq.eduservice.entity.frontVo.courseInfoVo;
import com.yyq.eduservice.service.EduChapterService;
import com.yyq.eduservice.service.EduCourseService;
import com.yyq.result.R;
import com.yyq.result.vo.courseInfoVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yyq
 * @create 2021-05-02 15:08
 * 前端１页面的查询
 */
@RequestMapping("/eduservice/front")
@RestController
@CrossOrigin
public class CourseFrontIndex {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private orderClient orderClient;

    //搜索课程
    @GetMapping("SearchCourse/{search}")
    public R searchCourse(@PathVariable String search){
        int total=0;
        List<EduCourse> list=courseService.searchCourse(search);
        Map<String,Object> map=new HashMap<>();
        if (list!=null){
            total=list.size();
        }
        map.put("items",list);
        map.put("total",total);
        return R.ok().data(map);

    }







    @PostMapping("getCourseFrontPages/{page}/{limit}")
    public R getCourseFrontPages(@PathVariable long page, @PathVariable long limit,
                                 @RequestBody(required = false) courseFrontVo courseFrontVo){
        Page<EduCourse> pages=new Page<>(page,limit);
        Map map=courseService.getCourseFrontPage(pages,courseFrontVo);
        return R.ok().data(map);
    }

    @GetMapping("getCourseFrontInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){

        //获取详细信息
        courseInfoVo info= courseService.getCourseFrontInfo(courseId);

        //获取章节小结信息
        List<chapter_1> getlist = chapterService.getlist(courseId);

        return R.ok().data("chapterList",getlist).data("CourseInfo",info);
    }

    @GetMapping("getOrder/{courseId}")
    public courseInfoVoOrder getCourseInfoOrder(@PathVariable String courseId){
        courseInfoVo info= courseService.getCourseFrontInfo(courseId);
        courseInfoVoOrder order=new courseInfoVoOrder();
        BeanUtils.copyProperties(info,order);
        return order;
    }


}
